/* 
 * File:   lua04.cpp
 * Author: Vicky.H
 * Email:  eclipser@163.com
 */
#include <iostream>
#include <lua.hpp> // -llua-5.1

int sum(lua_State* L) {
    int result = 0;
    int argc = lua_gettop(L);
    if (argc > 0) {
        for (int i = argc; i > 0; i--) {
            if (lua_isnumber(L, i)) {
                result += lua_tointeger(L, i);
            }
        }
    }
    return result;
}


/*
 * 
 */
int main(void) {

    printf("function: %p\n", &sum);
    lua_State* L = lua_open();
    luaL_openlibs(L);
    
    lua_register(L, "sum", sum);        // 将C函数主策到lua中
    luaL_dofile(L, "lua04.lua");
    
    lua_close(L);
    
    return 0;
}
